package com.qfmy.app.service.impl.novel;

import com.qfmy.app.mapper.novel.BookSelfMapper;
import com.qfmy.app.service.novel.BookSelfService;
import com.qfmy.app.vo.novel.BookSelfVo;
import com.qfmy.common.exception.BaseException;
import com.qfmy.common.login.LoginUser;
import com.qfmy.common.login.LoginUserHolder;
import com.qfmy.common.result.ResultCodeEnum;
import com.qfmy.model.entity.novel.BookSelf;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 清风明月
 * @Package com.qfmy.app.service.impl.novel
 * @date 2025/4/15
 * @description 书架
 */
@Service
@SuppressWarnings("all")
public class BookSelfServiceImpl implements BookSelfService {
    /**
     * 注入mapper
     */
    @Resource
    private BookSelfMapper bookSelfMapper;

    /**
     * 分页获取所有的加入书架的小说
     * @param pageNum
     * @param size
     * @return
     */
    @Override
    public List<BookSelfVo> getAll(int pageNum, int size) {
        //获取当前用户id
        Integer userId = getUserId();
        //计算分页
        int start = getStart(pageNum, size);
        //分页获取数据
        List<BookSelf> bookSelves =  bookSelfMapper.getAll(userId, start, size);
        //对数据进行拷贝
        List<BookSelfVo> bookSelfVos = CopyBookSelf(bookSelves);
        return bookSelfVos;
    }

    /**
     * 根据id删除书架中的小说
     * @param id
     */
    @Override
    public void deleteBookSelfById(Integer id) {
        //获取当前用户id
        Integer userId = getUserId();
        //根据id删除书架中的小说
        bookSelfMapper.deleteBookSelfById(id, userId);
    }

    /**
     * 添加小说到书架
     * @param bookId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBookSelf(Integer bookId) {
        //获取当前用户id
        Integer userId = getUserId();
        //创建书架对象
        BookSelf bookSelf = new BookSelf();
        //设置用户id
        bookSelf.setUserId(userId);
        //设置小说id
        bookSelf.setBookId(bookId);
        //添加小说到书架
        bookSelfMapper.addBookSelf(bookSelf);
    }

    /**
     * 对数据进行拷贝
     * @param bookSelves
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private List<BookSelfVo> CopyBookSelf(List<BookSelf> bookSelves) {
        //判断是否有数据
        if(bookSelves == null || bookSelves.size() == 0)
        {
            return new ArrayList<>();
        }
        //创建集合
        List<BookSelfVo> bookSelfVos = new ArrayList<>();
        //循环遍历数据
       for (BookSelf bookSelf : bookSelves) {
           //拷贝数据
           BookSelfVo bookSelfVo = new BookSelfVo();
           bookSelfVo.setId(bookSelf.getId());
           bookSelfVo.setBook(bookSelf.getBook());
           bookSelfVo.setChapter(bookSelf.getChapter());
           bookSelfVo.setLastChapterName(bookSelf.getLastChapterName());
           bookSelfVo.setLastChapterNumber(bookSelf.getLastChapterNumber());
           //将数据添加到集合中
           bookSelfVos.add(bookSelfVo);
       }
        return bookSelfVos;
    }

    /**
     * 计算分页
     * @param pageNum
     * @param size
     * @return
     */
    private int getStart(int pageNum, int size) {
        // 校验并调整分页参数
        pageNum = Math.max(pageNum, 1);
        size = Math.max(size, 10);
        //计算分页
        return (pageNum - 1) * size;
    }

    /**
     * 获取当前用户id
     */
    private Integer getUserId()
    {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if(loginUser == null)
        {
           throw new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        //获取用户id
        return loginUser.getUserId();
    }
}
